/**
 * 功能：
 * 	使视图与相关事件相分离
 *  类似于action 用来传递数据和控制视图
 *  quicktips 没有完成
 *  
 *  handler 事件需要重新改变量
 */
Ext.define('Risk.controller.rm.RmController',{
	extend : 'Ext.app.Controller',
	 stores:[
	      'rm.PmStore'
	 ],
	 models:[
	 	
	 ],
	      /**
	       * 视图在congtroller中进行注册
	       * 将控制权转交给 this Controller
	       */
	  	  //管理的视图
     views: [
            'rm.Pm',
            'rm.Pm2',
            'rm.RmResult'
    ],
    // 类创建好后要做的初始化工作
	init : function() {
	  this.control({
		  //类似于JQuery中的选择器   fieldLabel
	  	"pmfieldset > radiogroup":
	  	{
	  		change:this.pmRadioChange
	  	},
	  	"pm2fieldset > radiogroup":
	  	{
	  		change:this.pm2RadioChange
	  	},
		"pmfieldset #net":
	  	{
	  		change:this.textChange
	  	},"pmfieldset textfield":
	  	{
	  		change:this.pmResult
	  	},
	  	"pm2fieldset #net":
	  	{
	  		change:this.text2Change
	  	},"pm2fieldset textfield":
	  	{
	  		change:this.pm2Result
	  	},"rmpanel":{
	  		activate: this.getData
	  	}
	  });
	},
	pm2RadioChange:function(obj,newValue,oldValue,opts)
	{
		var flag = false;
		var temp = 0.00001;
		for(var i in newValue){	flag = (newValue[i]==1);}
		if(!flag)temp = null;
		obj.up('pm2fieldset').down("#net").setDisabled(flag);
		obj.up('pm2fieldset').down("#ks2").setValue(temp);
		obj.up('pm2fieldset').down("#ks1").setValue(temp);
		
	},
	pmRadioChange:function(obj,newValue,oldValue,opts)
	{
		var flag = false;
		var temp = 0.00001;
		for(var i in newValue){	flag = (newValue[i]==1);}
		if(!flag)temp = null;
		obj.up('pmfieldset').down("#net").setDisabled(flag);
		obj.up('pmfieldset').down("#ks2").setValue(temp);
		obj.up('pmfieldset').down("#ks1").setValue(temp);
		
	},
	text2Change:function(obj,newValue,oldValue)
	{
		
		//obj.up('pmfieldset').down("#net").setDisabled(flag);
		if(!obj.disabled){
		  obj.up('pm2fieldset').down("#ks2").setValue(newValue*0.12);
		  obj.up('pm2fieldset').down("#ks1").setValue(newValue*0.12);
		}
	},
	textChange:function(obj,newValue,oldValue)
	{
		
		//obj.up('pmfieldset').down("#net").setDisabled(flag);
		if(!obj.disabled){
		  obj.up('pmfieldset').down("#ks2").setValue(newValue*0.12);
		  obj.up('pmfieldset').down("#ks1").setValue(newValue*0.12);
		}
	},
	pmResult:function(obj)
	{
		if(!obj.disabled)
		{
			 var flag =  obj.up('pmfieldset').down("radio[checked=true]").inputValue;
			 var ks1 = obj.up('pmfieldset').down("#ks1").value;
			 var ks2 = obj.up('pmfieldset').down("#ks2").value;
			 var ks3 = obj.up('pmfieldset').down("#ks3").value;			 
			 var ks4 = obj.up('pmfieldset').down("#ks4").value;
			 if(flag==1)ks3*=0.1;
			 var kms = 1;
			 var pms =0;
			 try{
				 kms = ks1*ks2*ks3*ks4;
			 }catch(e){}
			 
			 if(kms<=0.013)
			 {
				 pms = 0.0001;
			 }else if(kms>0.013&&kms<=0.014)
			 {
				 pms = 0.001;
			 }else if(kms>0.014&&kms<=0.015)
			 {
				 pms = 0.003;
			 }else if(kms>0.015&&kms<=0.016)
			 {
				 pms = 0.005;
			 }else if(kms>0.016&&kms<=0.021)
			 {
				 pms = 0.01;
			 }else if(kms>0.021&&kms<=0.035)
			 {
				 pms = 0.1;
			 }else if(kms>0.035&&kms<=0.07)
			 {
				 pms = 0.5;
			 }else if(kms>0.07&&kms<=0.15)
			 {
				 pms = 0.9;
			 }else 
			 {
				 pms = 1;
			 }
			 obj.up('pmfieldset').down("#pmres").setValue(pms);
		}
	},
	pm2Result:function(obj)
	{
		if(!obj.disabled)
		{
			 var flag =  obj.up('pm2fieldset').down("radio[checked=true]").inputValue;
			 var ks1 = obj.up('pm2fieldset').down("#ks1").value;
			 var ks2 = obj.up('pm2fieldset').down("#ks2").value;
			 var ks3 = obj.up('pm2fieldset').down("#ks3").value;			 
			 var ks4 = obj.up('pm2fieldset').down("#ks4").value;
			 if(flag==1)ks3*=0.1;
			 var kms = 1;
			 var pms =0;
			 try{
				 kms = ks1*ks2*ks3*ks4;
			 }catch(e){}
			 
			 if(kms<=0.013)
			 {
				 pms = 0.0001;
			 }else if(kms>0.013&&kms<=0.014)
			 {
				 pms = 0.001;
			 }else if(kms>0.014&&kms<=0.015)
			 {
				 pms = 0.003;
			 }else if(kms>0.015&&kms<=0.016)
			 {
				 pms = 0.005;
			 }else if(kms>0.016&&kms<=0.021)
			 {
				 pms = 0.01;
			 }else if(kms>0.021&&kms<=0.035)
			 {
				 pms = 0.1;
			 }else if(kms>0.035&&kms<=0.07)
			 {
				 pms = 0.5;
			 }else if(kms>0.07&&kms<=0.15)
			 {
				 pms = 0.9;
			 }else 
			 {
				 pms = 1;
			 }
			 obj.up('pm2fieldset').down("#pmres").setValue(pms);
		}
	},
	 getData:function(obj)
		{
			var record = obj.up("maintab").ownerCt.ownerCt.items.get(0).getSelectionModel().selected.items[0];
			if(!obj.isLoad){
				obj.isLoad = true;
				Ext.Ajax.request({
				    url:'rm/get.do' ,
				    params:{projid:record.data.id,_doc:new Date().getMilliseconds()},
				    success:function(response){
				    //成功操作form 注入数据
//				    	var o = Ext.JSON.decode(response.responseText).others;
//				    	obj.getForm().setValues(o);
				    	var result = Ext.JSON.decode(response.responseText).success;
					    if(result)
					    {
					    	var o = Ext.JSON.decode(response.responseText).others;
					    	obj.getForm().setValues(o);
					    }else
					    {
					    	//obj.getForm().reset();
					    }
				    	
				    },
				    failure:function(response){
				     // 失败操作from 
				    	//obj.getForm().reset();
				    }
				});
			}
			
		}
	  
});